package cn.darkfire.web.Controller;

import cn.darkfire.web.Service.UserService;
import cn.darkfire.web.Service.impl.UserServiceImpl;
import cn.darkfire.web.Utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginByEmail.do")
public class LoginByEmailController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        String email = req.getParameter("email");
        String username = req.getParameter("username");
        String code = req.getParameter("code");
        if(email.length()<=1||username.length()<=1||code.length()<=4){
            resp.getOutputStream().print("信息输入错误!");
        }
        String token = DataUtils.getInstance().stringToMD5(username+code);
        UserService userService = new UserServiceImpl();
        if(req.getSession().getAttribute("token").equals(token)){
            req.getSession().setAttribute("users",userService.getAllUser());
            req.getSession().setAttribute("user",username);
            resp.getWriter().println("登录成功");
        }else{
            resp.getWriter().println("验证码输入错误！");
        }
    }
}
